package org.developer.server.rpc;

import java.util.HashMap;
import java.util.Map;

public class ServiceGetter {

    private static Map<Class,Object> serviceMap = new HashMap<>();

    public static<T> T getServiceByClazz(Class<T> clazz){
        try{
            //如果存在这个class，返回这个class的实例
            if(serviceMap.containsKey(clazz)){
                return (T) serviceMap.get(clazz);
            }else{
                //否则新建该class的实例，并且返回实例
                T bean = clazz.newInstance();
                serviceMap.put(clazz,bean);
                return bean;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
